package com.pig4cloud.pig.module.controller.product;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.DateUtils;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.mer.Mer;
import com.pig4cloud.pig.module.api.entity.product.MallSecondKill;
import com.pig4cloud.pig.module.api.entity.product.Product;
import com.pig4cloud.pig.module.service.mer.IMerService;
import com.pig4cloud.pig.module.service.product.IMallSecondKillService;
import com.pig4cloud.pig.module.service.product.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 秒杀列Controller
 *
 * @author ruoyi
 * @date 2022-11-05
 */
@Validated
@Api(value = "秒杀列控制器", tags = {"秒杀列管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/second/kill")
public class MallSecondKillController {

    private final IMallSecondKillService mallSecondKillService;
	private final IMerService merService;
	private final IProductService productService;
	@Value("${ruoyi.imgUrl}")
	private String imgUrl;


    /**
     * 查询秒杀列列表
     */
    @ApiOperation("查询秒杀列列表")
    @PreAuthorize("@pms.hasPermission('module:secondKill:list')")
    @GetMapping("/list")
    public TableDataInfo<MallSecondKill> list(@Validated MallSecondKill mallSecondKill) {
        QueryWrapper<MallSecondKill> qw = new QueryWrapper<>();

        TableDataInfo<MallSecondKill> td = PageUtils.buildDataInfo(mallSecondKillService.page(PageUtils.buildPage(),qw));
        List<MallSecondKill> list = td.getData();
        return td;
    }

    /**
     * 获取秒杀列详细信息
     */
    @ApiOperation("获取秒杀列详细信息")
    @PreAuthorize("@pms.hasPermission('module:secondKill:query')")
    @GetMapping("/{id}")
    public R<MallSecondKill> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        MallSecondKill mallSecondKill = mallSecondKillService.getById(id);
		if(mallSecondKill!=null){
			if(StrUtil.isNotEmpty(mallSecondKill.getActiveImg())){
				mallSecondKill.setActiveImgPath( imgUrl + mallSecondKill.getActiveImg());
			}

			if(mallSecondKill.getLinkId() != null && mallSecondKill.getLinkId() > 0){
				Product product = productService.goodsDetail(Long.parseLong(mallSecondKill.getLinkId()+""));
				mallSecondKill.setProduct(product);
			}

			if(mallSecondKill.getCateChildId()!=null && mallSecondKill.getCateChildId() > 0){
				QueryWrapper<Mer> qw = new QueryWrapper<>();
				if(mallSecondKill.getCateChildId()!=null){
					qw.eq("cate_child_id",mallSecondKill.getCateChildId());
				}
				List<Mer> list = merService.list(qw);
				list.forEach(mer1 -> {
					if(StrUtil.isNotEmpty(mer1.getShopLogo())){
						mer1.setShopLogoPath(imgUrl + mer1.getShopLogo());
					}else{
						mer1.setShopLogoPath("");
					}
					QueryWrapper<Product> qwPro = new QueryWrapper<>();
					qwPro.eq("mer_id",mer1.getId());
					qwPro.eq("is_show",1);
					long productCount = productService.count(qwPro);
					mer1.setProductCount(productCount);
				});
				mallSecondKill.setMerList(list);
			}

			if(mallSecondKill.getBeginTime()!=null && mallSecondKill.getEndTime() !=null) {
				String[] dateRange = {DateUtils.getDayByDate(mallSecondKill.getBeginTime()),DateUtils.getDayByDate(mallSecondKill.getEndTime())} ;
				mallSecondKill.setDateRange(dateRange);
			}
		}
        return R.ok(mallSecondKill);
    }

    /**
     * 新增秒杀列
     */
    @ApiOperation("新增秒杀列")
    @PreAuthorize("@pms.hasPermission('module:secondKill:add')")
    @SysLog("秒杀列")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody MallSecondKill mallSecondKill) {
        return R.okByRow(mallSecondKillService.save(mallSecondKill) ? 1 : 0);
    }

    /**
     * 修改秒杀列
     */
    @ApiOperation("修改秒杀列")
    @PreAuthorize("@pms.hasPermission('module:secondKill:edit')")
    @SysLog("秒杀列")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody MallSecondKill mallSecondKill) {
        return R.okByRow(mallSecondKillService.updateById(mallSecondKill) ? 1 : 0);
    }

    /**
     * 删除秒杀列
     */
    @ApiOperation("删除秒杀列")
    @PreAuthorize("@pms.hasPermission('module:secondKill:remove')")
    @SysLog("秒杀列")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(mallSecondKillService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
